package com.infocollect.project.api.ui;

import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappPowerMenuEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.helper.JSONUtil;
import com.infocollect.project.api.service.ITappPowerMenuService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import com.infocollect.project.form.vo.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (TappPowermenu)表控制层
 *
 * @author makejava
 * @since 2019-12-25 10:32:29
 */
@Api(value = "tappPowermenu", tags = "权限菜单信息表")
@RestController
@RequestMapping("tappPowermenu")
public class TappPowermenuController {
    @Autowired
    private TokenService tokenService;
    /**
     * 服务对象
     */
    @Resource
    private ITappPowerMenuService tappPowermenuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public R<TappPowerMenuEntity> selectOne(Long id) {
        return  R.newOk( this.tappPowermenuService.queryById(id));
    }

    /**
     *
     * @param param
     * @return
     * @throws IOException
     */
    @PostMapping("SavePower")
    @ResponseBody
    public R<String> SavePower(@RequestBody Map<String,Object> param)throws Exception{
        R<String> result=new R<>();
        Long roleID =Long.parseLong(param.get("RoleFID").toString());
       String powerParamJson= param.get("PowerParam").toString();
        List<String> paramlist= JSONUtil.jsonToObjList(String.class,powerParamJson);
        System.out.println(paramlist.size());
        tappPowermenuService.deleteByRoleId(roleID);
//        CurrentUserInfoVO currentUserInfo= CurrentHelp.Ins.GetUser();
        LoginUser currentUserInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        for(int i=0;i<paramlist.size();i++){
            String ee=paramlist.get(i);
            TappPowerMenuEntity menuPowerEntity = new TappPowerMenuEntity();
            menuPowerEntity.setFcreateby(currentUserInfo.getUserName());
            menuPowerEntity.setFlastupdateby(currentUserInfo.getUserName());
            menuPowerEntity.setFcreationdate(new Date());
            menuPowerEntity.setFlastupdatedate(new Date());
            menuPowerEntity.setFmenuid(Long.parseLong(ee));
            menuPowerEntity.setFroleid(roleID);
            tappPowermenuService.insert(menuPowerEntity);
        }
        return R.newOk();
    }
}